<?php
namespace app\api\controllers;

use app\models\Old;
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use app\api\forms\OldSearchForm;
/**
 * 仪器控制器
 * Class InsController
 */
class InsController extends ControllerBase
{

    public function initialize(){
        parent::initialize();
        $this->assets->addJs('pc/js/setwindow.js');
        $this->view->setTemplateBefore('information');
    }

    public function oldAction()
    {
        $this->view->title = '折旧台账';
        $numberPage = 1;

        if ($this->request->isPost()) {
            $this->persistent->parameters = $this->request->hasPost('reset') ? []:$this->request->getPost();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $form = new OldSearchForm();
        $this->persistent->parameters?$form->bind($this->persistent->parameters,$form):$form->clear();
        $query = $form->getQuery();
        $inss = $query->execute();

        $paginator = new Paginator([
            'data' => $inss,
            'limit'=> 5,
            'page' => $numberPage
        ]);

        $this->view->page = $paginator->getPaginate();
        $this->view->form = $form;
    }

    public function testAction()
    {
        $this->response->setHeader("Content-Type", "application/json");
        $inss= Old::findFirst();
        $metadata = $inss->getModelsMetaData();
        $res = $metadata->getColumnMap($inss);
        $this->view->disable();
        echo json_encode($res);
    }
}